package com.cxrj.zhsq.activityManage.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.activityManage.dao.PropertyActivityMapper;
import com.cxrj.zhsq.activityManage.dao.PropertyActivityTypeMapper;
import com.cxrj.zhsq.activityManage.entity.PropertyActivity;
import com.cxrj.zhsq.activityManage.entity.PropertyActivityType;
import com.cxrj.zhsq.activityManage.service.ActivityService;

@Service
public class ActivityServiceImpl implements ActivityService {

	@Resource
	private PropertyActivityTypeMapper propertyActivityTypeMapper;

	@Resource
	private PropertyActivityMapper propertyActivityMapper;

	@Override
	public Map<String, Object> insertActivityType(PropertyActivityType propertyActivityType) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if (propertyActivityType.getId().equals("")) {
			propertyActivityType.setId(UUID.randomUUID().toString());
			propertyActivityType.setStatus("1");
			propertyActivityType.setCreateTime(new Date());
			c = propertyActivityTypeMapper.insertSelective(propertyActivityType);
		} else {
			c = propertyActivityTypeMapper.updateByPrimaryKeySelective(propertyActivityType);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> seleteActivityTypePage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		recordsTotal = propertyActivityTypeMapper.selectByCount(sqlMap) + "";
		List<PropertyActivityType> list = propertyActivityTypeMapper.selectPropertyActivityTypesPage(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> updateActivityType(PropertyActivityType propertyActivityType) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = propertyActivityTypeMapper.updateByPrimaryKeySelective(propertyActivityType);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> seleteActivityTypeById(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		PropertyActivityType propertyActivityType = propertyActivityTypeMapper.selectByPrimaryKey(id);
		resultMap.put("activityType", propertyActivityType);
		return resultMap;
	}

	@Override
	public Map<String, Object> insertActivity(PropertyActivity propertyActivity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if (propertyActivity.getId().equals("")) {
			propertyActivity.setId(UUID.randomUUID().toString());
			propertyActivity.setStatus("1");
			propertyActivity.setCreateTime(new Date());
			c = propertyActivityMapper.insertSelective(propertyActivity);
		} else {
			c = propertyActivityMapper.updateByPrimaryKeySelective(propertyActivity);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> seleteActivityPage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		recordsTotal = propertyActivityMapper.selectByCount(sqlMap) + "";
		List<PropertyActivity> list = propertyActivityMapper.selectPropertyActivitysPage(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> updateActivity(PropertyActivity propertyActivity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = propertyActivityMapper.updateByPrimaryKeySelective(propertyActivity);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> seleteActivityById(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		PropertyActivity propertyActivity = propertyActivityMapper.selectByPrimaryKey(id);
		resultMap.put("activity", propertyActivity);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectActivityTypeByAll(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		List<PropertyActivityType> list = propertyActivityTypeMapper.selectPropertyActivityTypeByAll(sqlMap);
		resultMap.put("list", list);
		return resultMap;
	}
}
